package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.Product;
import com.spzx.product.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author gax
 * @since 2025-08-23
 */

@Tag(name = "商品")
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;

    @Operation(summary = "查询商品列表")
    @GetMapping("list")
    public TableDataInfo list() {
        startPage();
        List<Product> list = productService.selectProductList();
        return getDataTable(list);
    }

    @Operation(summary = "保存商品(spu，sku 列表，spu 详情，库存)")
    // 因为网关的局部过滤器会去截掉一位路径
    @PostMapping("")
    public AjaxResult saveProduct(@RequestBody Product product) {
        productService.saveProduct(product);
        return success();
    }

    @Operation(summary = "商品上架或下架")
    @GetMapping("updateStatus/{spuId}/{status}")
    public AjaxResult updateStatus(@PathVariable Long spuId,@PathVariable Integer status) {
        if(status != 1 && status != -1){
            //throw new ServiceException("状态值有误！");
            return AjaxResult.error("非法参数");
        }
        productService.updateStatus(spuId,status);
        return success();
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("{ids}")
    public AjaxResult remove(
            @Parameter(description = "商品的 id 列表")
            @PathVariable List<Long> ids) {
        return toAjax(productService.deletePorductByIds(ids));
    }
}
